//########################################################################
//## (C) by Edwin Vriethoff under Microsoft Reciprocal License (Ms-RL)  ##
//## See http://www.codeplex.com/SPUserPoll/license for full license    ##
//##                                                                    ##
//## http://edwin.vriethoff.net http://www.codeplex.com/SPUserPoll/     ##
//########################################################################

using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;

namespace SPUserPoll.Presentation.FieldTypes
{
    class SPUserPollLookupField : BaseFieldControl
    {

        private DropDownList _answersDropDown;
        private Literal _answerValue;
        private string _pollName;

        /// <summary>
        /// Create a SPUserPoll lookup field
        /// </summary>
        /// <param name="pollName"></param>
        public SPUserPollLookupField(string pollName)
        {
            PollName = pollName;
        }

        /// <summary>
        /// Get and set the Poll Name
        /// </summary>
        private string PollName
        {
            get { return _pollName; }
            set { _pollName = value; }
        }
         
        /// <summary>
        /// Get the value
        /// </summary>
        public override object Value
        {
            get
            {
                if (ControlMode == SPControlMode.New || ControlMode == SPControlMode.Edit)
                {
                    return _answersDropDown.Text;
                }
                else
                {
                    return ListItemFieldValue;
                }
            }
            set
            {
                base.ItemFieldValue = value;
            }
        }

        /// <summary>
        /// Override the oninit
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            EnsureChildControls();
        }


        /// <summary>
        /// Add controls
        /// </summary>
        protected override void CreateChildControls()
        {
            //Clear all existing controls
            Controls.Clear();

            //Call the basefunction
            base.CreateChildControls();


            //In edit mode show the dropdownlist
            if (ControlMode == SPControlMode.New || ControlMode == SPControlMode.Edit)
            {
                _answersDropDown = new DropDownList();

                //Get all listitems for the current folder
                Data.Components.AnswersList answerList = new SPUserPoll.Data.Components.AnswersList();
                List<ListItem> listItems = answerList.GetPollAnswerDropDownItems(PollName);

                //get selected item id
                Microsoft.SharePoint.SPFieldLookupValue fieldValue = (Microsoft.SharePoint.SPFieldLookupValue) ListItemFieldValue;

                foreach (ListItem item in listItems)
                {
                    //Select the item if it is the current id
                    if (fieldValue != null && fieldValue.LookupId.ToString() == item.Value)
                        item.Selected = true;

                    //Add the item
                    _answersDropDown.Items.Add(item);
                }

                //Add the dropdownlist
                Controls.Add(_answersDropDown);
            }
            else
            {
                //Show literal with the value
                _answerValue = new Literal();
                _answerValue.Text = Convert.ToString(ListItemFieldValue);
                
                //Add the literal
                Controls.Add(_answerValue);
            }
        }
    }
}